/*
 * #%L
 * SwiftUICommonUtilities
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.export.excel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.codehaus.plexus.util.StringUtils;

/**
 * Used to attach Files in email...
 * 
 * @author ashwin kumar
 * 
 */
public class FileDataSource implements javax.activation.DataSource {
	private String contentType = "application/excel";
	private String name = null;
	private ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
	private String fileName = "";

	public FileDataSource() {

	}

	/**
	 * @param fileName
	 */
	public FileDataSource(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @param fileName
	 *            the fileName to set
	 */
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setName(String name) {
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.activation.DataSource#getInputStream()
	 */
	public InputStream getInputStream() {
		if (StringUtils.isBlank(fileName)) {
			return new ByteArrayInputStream(arrayOutputStream.toByteArray());
		} else {
			try {
				return new FileInputStream(fileName);
			} catch (FileNotFoundException e) {
				return null;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.activation.DataSource#getOutputStream()
	 */
	public OutputStream getOutputStream() {
		if (StringUtils.isBlank(fileName)) {
			return arrayOutputStream;
		} else {
			try {
				return new FileOutputStream(fileName);
			} catch (FileNotFoundException e) {
				return null;
			}
		}
	}

	public String getContentType() {
		return contentType;
	}

	public String getName() {
		return name;
	}

}
